package com.css.dzj.zfjg.modules.operatingabnormal.controller;

import com.css.aq.server.service.BaseService;
import com.css.dzj.zfjg.common.util.R;
import com.css.dzj.zfjg.modules.jcdx.entity.JgLhjgJcdxEntity;
import com.css.dzj.zfjg.modules.operatingabnormal.entity.JgLhjgOperatingAbnormalEntity;
import com.css.dzj.zfjg.modules.operatingabnormal.service.JgLhjgOperatingAbnormalService;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * 企业经营异常名录信息
 *
 * @author liyuan
 */
@RestController
@RequestMapping("generator/jglhjgoperatingabnormal")
public class JgLhjgOperatingAbnormal {
    @Autowired
    private JgLhjgOperatingAbnormalService abnormalService;

    @Autowired
    private BaseService baseService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam(required = false) String enterpriseName,
                  @RequestParam Integer pageSize, @RequestParam Integer pageNo){
        try {
            PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
            QueryResults<JgLhjgOperatingAbnormalEntity> page = abnormalService.queryPage(enterpriseName, pageRequest);
            baseService.logSuccess(3, "分页查询企业经营异常名录信息成功", "2");
            return R.ok().put("page", page);
        } catch (Exception e) {
            e.printStackTrace();
            baseService.logFailed(3, "分页查询企业经营异常名录信息失败", "2");
            return R.error(e.getMessage());
        }
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{operatingAbnormalId}")
    public R info(@PathVariable("operatingAbnormalId") String operatingAbnormalId){
        try {
            JgLhjgOperatingAbnormalEntity jgLhjgOperatingAbnormal = abnormalService.getById(operatingAbnormalId);
            baseService.logSuccess(3, "通过id获取企业经营异常名录信息成功", "2");
            return R.ok().put("jgLhjgOperatingAbnormal", jgLhjgOperatingAbnormal);
        } catch (Exception e) {
            e.printStackTrace();
            baseService.logFailed(3, "通过id获取企业经营异常名录信息失败", "2");
            return R.error(e.getMessage());
        }
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody JgLhjgOperatingAbnormalEntity jgLhjgOperatingAbnormal){
        try {
            abnormalService.save(jgLhjgOperatingAbnormal);
            baseService.logSuccess(3, "企业经营异常名录信息添加成功", "2");
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            baseService.logFailed(3, "企业经营异常名录信息添加失败", "2");
            return R.error(e.getMessage());
        }
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody JgLhjgOperatingAbnormalEntity jgLhjgOperatingAbnormal){
        try {
            abnormalService.updateById(jgLhjgOperatingAbnormal);
            baseService.logSuccess(3, "企业经营异常名录信息修改成功", "2");
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            baseService.logFailed(3, "企业经营异常名录信息修改失败", "2");
            return R.error(e.getMessage());
        }
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody String[] operatingAbnormalIds){
        try {
            abnormalService.removeByIds(Arrays.asList(operatingAbnormalIds));
            baseService.logSuccess(3, "企业经营异常名录信息删除成功", "2");
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            baseService.logFailed(3, "企业经营异常名录信息删除失败", "2");
            return R.error(e.getMessage());
        }
    }

}
